//
//  FindViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindViewController.h"
#import "PhoneixNews.pch"
#import "FindModel.h"
#import "FindTableViewCell.h"
#import "FindCollectionViewCell.h"
@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mubtaleArray;//金币商城
@property (nonatomic, retain) NSMutableArray *LoadArray;//页面
@end

@implementation FindViewController
- (void)dealloc {
    [_tableView release];
    [_mubtaleArray release];
    [_LoadArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style: UITableViewStyleGrouped];
    
    [self.tableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"cell"];
    

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [self getData];
}
#pragma 解析数据
- (void)getData {
     AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:Find parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"recommend"];
        self.mubtaleArray = [FindModel modelHanderWithArray:array];
        NSArray *arrayTwo = responseObject[@"stream"];
        
        self.LoadArray = [FindModel modelHanderWithArray:arrayTwo];
       
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark -TableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _LoadArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     FindModel *finde = _LoadArray[indexPath.row];
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:finde.image]];
    [cell.imageViewTwo sd_setImageWithURL:[NSURL URLWithString:finde.icon]];
    cell.lable.text = finde.name;
    cell.title.text = finde.title;
    cell.subtitle.text = finde.subtitle;
    return cell;
}
// TableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FindModel *find = _LoadArray[indexPath.row];
    DetailePageViewController *detail = [[DetailePageViewController alloc] init];
    if (find.image) {
        detail.urlStr = find.jumpUrl;
        [self.navigationController pushViewController:detail animated:YES];
    }
   
}
// 头试图高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 400;
}
// 头试图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 250;
}
// cell头试图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    headView.backgroundColor = [UIColor redColor];
   
    
    
    UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc] init];
    layou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layou.itemSize = CGSizeMake(50, 900/ 6);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 180) collectionViewLayout:layou];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [headView addSubview:collectionView];
    
   
    return headView;
}

#pragma mark CollectionView
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mubtaleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FindModel *homePage = self.mubtaleArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:homePage.icon]];
    cell.lable.text = homePage.title;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FindModel *homePage = self.mubtaleArray[indexPath.row];
    DetailePageViewController *detaile = [[DetailePageViewController alloc] init];
    detaile.urlStr = homePage.url;
    [self.navigationController pushViewController:detaile animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
